package com.hero.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hero.entity.Organization;
import com.hero.dao.OrganizationDao;
import com.hero.service.OrganizationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织机构表(Organization)表服务实现类
 *
 * @author makejava
 * @since 2021-06-29 19:38:18
 */
@Service("organizationService")
public class OrganizationServiceImpl implements OrganizationService {
    @Resource
    private OrganizationDao organizationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Organization queryById(Integer id) {
        return this.organizationDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Organization> queryAllByLimit(int offset, int limit) {
        return this.organizationDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<Organization> queryAll(Organization organization) {
        return organizationDao.queryAll(organization);
    }

    @Override
    public Map<String, Object> find(Integer pageNum, Integer pageSize,Organization organization) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Organization> pageInfo = new PageInfo<>(organizationDao.queryAll(organization));
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        map.put("pages", pageInfo.getPages());
        map.put("isFirst", pageInfo.isIsFirstPage());
        map.put("isLast", pageInfo.isIsLastPage());
        map.put("pageNum", pageInfo.getPageNum());
        map.put("pageSize", pageInfo.getPageSize());
        return map;
    }

    /**
     * 新增数据
     *
     * @param organization 实例对象
     * @return 实例对象
     */
    @Override
    public Organization insert(Organization organization) {
        this.organizationDao.insert(organization);
        return organization;
    }

    /**
     * 修改数据
     *
     * @param organization 实例对象
     * @return 实例对象
     */
    @Override
    public Organization update(Organization organization) {
        this.organizationDao.update(organization);
        return this.queryById(organization.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.organizationDao.deleteById(id) > 0;
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        for (Integer id : ids){
            deleteById(id);
        }
        return true;
    }
}
